
import database.Reservation;
import database.Restaurant;
import database.user;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Random;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author karolos
 */
//klasi nimatos
public class reservationthread extends Thread {

    private ArrayList<Reservation>aitiseiskratisis;
    private ArrayList<Reservation>prosepiv;
    private ArrayList<Reservation>epivaiveomenes;
    private ArrayList<Restaurant>estiatoria;
    private ArrayList<user>user;
    private Random random=new Random();
    private boolean end;

    public reservationthread() throws SQLException {
    super("ypiresia kratiseon");
    DBManager db=mainframe.getDB();
    estiatoria=db.getallrestaurants();
    user=db.getallusers();
    aitiseiskratisis=new ArrayList<Reservation>();
    prosepiv=new ArrayList<Reservation>();
    epivaiveomenes=new ArrayList<Reservation>();

    }

     public void addReservation(Reservation k){
        aitiseiskratisis.add(k);
    }

    public boolean isEnd() {
        return end;
    }

    public void setEnd(boolean end) {
        this.end = end;
    }

     @Override
    public void run() {

        int sleepTime;

        try {


            while (true) {
                 //tyxaios xronos sleep
                sleepTime = random.nextInt(2000) + 2000;

                Thread.sleep(sleepTime);

                int k = random.nextInt(20) + 1;
                int l = random.nextInt(20) + 1;


              for(int i = 0; i < k; i++){
                    int m = random.nextInt(estiatoria.size());
                    int n = random.nextInt(user.size());
                    prosepiv.add(new Reservation(user.get(n),estiatoria.get(m)));
                }

                for(int i = 0; i < l; i++){
                    int m = random.nextInt(estiatoria.size());
                    int n = random.nextInt(user.size());
                    epivaiveomenes.add(new Reservation(user.get(n),estiatoria.get(m)));
                }

                for(int i = 0; i < aitiseiskratisis.size(); i++){
                    if(aitiseiskratisis.get(i).getState() == 0){
                        if(epivaiveomenes.size() < 10){
                          if(prosepiv.size() < 10){
                          aitiseiskratisis.get(i).setState(3);
                          prosepiv.add(aitiseiskratisis.get(i));
                           aitiseiskratisis.remove(i);
                            }
                            else{   //anamoni
                                aitiseiskratisis.get(i).setState(1);
                            }
                        }


                        else{  //den yparxei dia8esimotita
                            aitiseiskratisis.get(i).setState(2); 
                            aitiseiskratisis.remove(i);
                        }
                    }
                }

                for(int i = 0; i < prosepiv.size(); i++){
                    if(prosepiv.get(i).getState() == 4){
                        if(epivaiveomenes.size() < 10){
                            prosepiv.get(i).setState(6); //epikiro8ike i kratisi
                            epivaiveomenes.add(prosepiv.get(i));
                            prosepiv.remove(i);
                        }
                        else{ //akyrosi kratisis
                            prosepiv.get(i).setState(5); 
                            prosepiv.remove(i);
                        }
                    }
                    else{//akyrosi kratisis
                        prosepiv.get(i).setState(5); 
                        prosepiv.remove(i);
                    }
                }


                sleepTime = random.nextInt(2000) + 2000;

                Thread.sleep(sleepTime);

                for(int i = 0; i < aitiseiskratisis.size(); i++){
                    if(aitiseiskratisis.get(i).getState() == 1){
                        if(epivaiveomenes.size() < 10){
                            if(prosepiv.size() < 10){ //dia8esimi kratisi
                                aitiseiskratisis.get(i).setState(3); 
                                prosepiv.add(aitiseiskratisis.get(i));
                                aitiseiskratisis.remove(i);
                            }
                        }
                    }
                }


                sleepTime = random.nextInt(2000) + 2000;

                Thread.sleep(sleepTime);

                int m = random.nextInt(epivaiveomenes.size());
                for(int i = 0; i < (m)-1; i++){
                    epivaiveomenes.remove(0);
                }

                if(end)
                    return;
            }
        } catch (InterruptedException ex) {

        }

    }

   


}
